from pymongo import MongoClient

# 源client
client = MongoClient('mongodb://admin:yzy%40123456@10.11.16.1:27017/')
# 源数据库
origin_db = client["report_madicine"]
# 目标数据库
target_db = client["report_madicine"]
# 源document
origin_collection = origin_db["healthCare"]
# 目标document
target_collection = target_db["healthCare_copy"]
#如果已经存在，则删除collection
target_collection.drop()

# 原始数据总数
origin_count = origin_collection.count_documents({})
print("原始数据总数:")
print(origin_count)

# 每次跳过多少
skip_count = 0
# 每次查询300条记录
limit = 300
while skip_count < origin_count:
	data = origin_collection.find().skip(skip_count).limit(limit)
	target_collection.insert_many(data)
	skip_count += limit
	print(skip_count)
 
# 获取已经同步的总记录
result_count = target_collection.count_documents({})

# 如果小于原始数据总数，则同步剩余的数据
if(result_count < origin_count):
	data = origin_collection.find().skip(result_count).limit(origin_count - result_count)
	target_collection.insert_many(data)

print("同步总数:")
print(result_count)